package com.power.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.power.constant.BusinessEnum;
import com.power.domain.MemberCollection;
import com.power.domain.Prod;
import com.power.ex.handle.BussinessException;
import com.power.feign.MemberProdFrign;
import com.power.mapper.MemberCollectionMapper;
import com.power.model.Result;
import com.power.service.MemberCollectionService;
import com.power.util.AuthUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author dingchunlin
* @description 针对表【member_collection】的数据库操作Service实现
* @createDate 2024-07-10 15:12:13
*/
@Service
public class MemberCollectionServiceImpl extends ServiceImpl<MemberCollectionMapper, MemberCollection> implements MemberCollectionService{

    @Autowired
    private MemberCollectionMapper memberCollectionMapper;

    @Autowired
    private MemberProdFrign memberProdFrign;

    /**
     * 查询会员收藏的商品数量
     * @return
     */
    @Override
    public Long queryMemberCollectionProdCount() {
        //获取会员的openid
        String openId = AuthUtils.getMemberOpenId();
        // 根据openid查询会员收藏的商品数量
        Long count = memberCollectionMapper.selectCount(new LambdaQueryWrapper<MemberCollection>()
                .eq(MemberCollection::getOpenId, openId));

        return count;
    }

    /**
     * 分页查询会员收藏的商品
     *
     * @param memberOpenId 会员的openid
     * @param current      当前页
     * @param size         每页大小
     * @return
     */
    @Override
    public Page<Prod> queryMemberCollectionProdsPageByOpenId(String memberOpenId, Long current, Long size) {
        // 创建商品分页对象
        Page<Prod> prodPage = new Page<>(current, size);
        // 创建收藏商品的分页对象
        Page<MemberCollection> page = new Page<>(current, size);
        // 根据会员的openid查询会员收藏的商品
        page = memberCollectionMapper.selectPage(page, new LambdaQueryWrapper<MemberCollection>()
               .eq(MemberCollection::getOpenId, memberOpenId)
               .orderByDesc(MemberCollection::getCreateTime)
        );
        // 获取分页的对象
        List<MemberCollection> collectionList = page.getRecords();
        // 判断是否有值
        if (collectionList.isEmpty() || collectionList.size() == 0) {
            return prodPage;
        }
        // 获取收藏的商品id列表
        List<Long> prodIdList = collectionList.stream().map(MemberCollection::getProdId).collect(Collectors.toList());
        // 根据商品id列表查询商品
        Result<List<Prod>> result = memberProdFrign.getProdListByIds(prodIdList);
        // 判断是否有值
        if (result.getCode().equals(BusinessEnum.OPERATION_FAIL.getCode())){
            throw new BussinessException("远程服务调用：根据商品id列表查询商品失败");
        }
        // 获取商品列表
        List<Prod> prodList = result.getData();
        // 设置商品分页对象
        prodPage.setRecords(prodList);
        // 设置总数
        prodPage.setTotal(page.getTotal());
        // 设置总页数
        prodPage.setPages(page.getPages());
        // 返回商品分页对象
        return prodPage;
    }

    /**
     * 判断会员是否收藏了该商品
     * @param memberOpenId 会员的openid
     * @param prodId       商品的id
     * @return true表示收藏了，false表示未收藏
     */
    @Override
    public Boolean isCollection(String memberOpenId, Long prodId) {
        Long count = memberCollectionMapper.selectCount(new LambdaQueryWrapper<MemberCollection>()
                .eq(MemberCollection::getOpenId, memberOpenId)
                .eq(MemberCollection::getProdId, prodId));
        return count > 0;
    }

    /**
     * 添加或取消收藏商品
     *
     * @param memberOpenId 会员的openid
     * @param prodId       商品的id
     * @return true表示添加收藏成功，false表示取消收藏成功
     */
    @Override
    public void addOrCancelProdCollection(String memberOpenId, Long prodId) {
        // 首先根据openid和商品id查询会员收藏的记录
        MemberCollection memberCollection = memberCollectionMapper.selectOne(new LambdaQueryWrapper<MemberCollection>()
                .eq(MemberCollection::getOpenId, memberOpenId)
                .eq(MemberCollection::getProdId, prodId));
        // 判断是否有值
        if (ObjectUtil.isEmpty(memberCollection)) {
            // 未收藏，则添加收藏
            MemberCollection collection = new MemberCollection();
            collection.setOpenId(memberOpenId);
            collection.setProdId(prodId);
            collection.setCreateTime(new Date());
            memberCollectionMapper.insert(collection);
        } else {
            // 已收藏，则删除收藏
            memberCollectionMapper.delete(new LambdaQueryWrapper<MemberCollection>()
                    .eq(MemberCollection::getOpenId, memberOpenId)
                    .eq(MemberCollection::getProdId, prodId));
        }
    }
}




